// 引入axios组件
import axios from 'axios';
import qs from 'qs'; // 处理数据  qs.stringify(params)
// 数据删除之后  需要重新请求页面  不建议用路由  用监听自动加载~
import { checkRule } from '@/assets/js/public';
// 引入公共js文件
export default {
  data() {
    return {
      modal1: false,
      search: '',
      addbrand: false,
      role_name: '',
      roleid: '',
      putpage: '',
      columns7: [
        {
          type: 'selection',
          width: 60,
          align: 'center',
        },
        {
          title: '角色名称',
          align: 'center',
          key: 'username',
          render: (h, params) => h('div', [h('strong', params.row.title)]),
        },
        {
          title: '操作选项',
          key: 'action',
          // width: 150,
          align: 'center',
          render: (h, params) =>
            h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'success',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.authDisbute(params.row.id);
                      // this.show(params);
                    },
                  },
                },
                '分配权限',
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'info',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.show(params.row.id);
                      // this.show(params);
                    },
                  },
                },
                '编辑',
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.remove(params);
                    },
                  },
                },
                '删除',
              ),
            ]),
        },
      ],
    };
  },
  created() {
    const self = this;
    this.$Spin.show();
    setTimeout(() => {
      this.$Spin.hide();
    }, 500);
    const spage = this.$route.query.page;
    const asearch = this.$route.query.search;
    axios({
      method: 'post',
      url: '/admin/admin/adminRoleList',
      data: { page: spage, search: asearch },
    })
      .then((response) => {
        checkRule(response, self); // 检查是否有权限
        self.putpage = spage;
        self.$store.commit('adminRolelistinfo', response.data);
      })
      .catch((response) => {});
  },
  // 用来获取VUEX中的文件
  computed: {
    data6() {
      return this.$store.state.admin.adminRoleinfo.roleList;
    },
    page() {
      return this.$store.state.admin.adminRoleinfo.roleCount;
    },
  },
  methods: {
    // 分配权限
    authDisbute(index) {
      this.$router.push({
        path: '/authDisbute', // 这是路由名称
        query: { roleid: index },
      });
    },
    // 添加角色
    addRole() {
      this.role_name = '';
      this.addbrand = true;
      this.roleid = '';
    },
    // 修改角色
    show(index) {
      const self = this;
      axios({
        method: 'post',
        url: '/admin/admin/adminRoleEdit',
        data: { roleid: index },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          self.role_name = response.data.roleinfo.title;
          self.roleid = response.data.roleinfo.id;
        })
        .catch((response) => {
          // self.$Message.error('请求失败');
        });
      this.addbrand = true;
    },
    // 执行添加修改角色
    ok_addRole() {
      const self = this;
      axios({
        method: 'post',
        url: '/admin/admin/adminRoleUpdate',
        data: { role_name: this.role_name, roleid: this.roleid },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限

          if (response.data.code == 1) {
            self.$Message.success(response.data.msg);
            self.$store.commit('adminRoleInfoAgain', self.putpage, self.search);
            // self.data6.splice(param.index, 1);//如果把那个1改成0，就是不删除
          } else {
            self.$Message.error(response.data.msg);
          } //* /
        })
        .catch((response) => {
          // self.$Message.error('请求失败');
        });
    },
    // 点击取消之后走的方法
    cancel_addRole() {
      this.addbrand = false;
    },
    // 分页的钩子函数，当跳页的时候重新请求（将页码发给后台）
    getpage(page) {
      const self = this;
      if (page == undefined) {
        page = 1;
      }
      this.putpage = page;
      // 网站设置页
      axios({
        method: 'post',
        url: '/admin/admin/adminRoleList',
        data: {
          page,
          search: self.search,
        },
      })
        .then((response) => {
          self.$store.commit('adminRolelistinfo', response.data);
        })
        .catch((response) => {
          console.log(response);
        });
      this.$router.push({
        path: '/adminrole', // 这是路由名称
        query: { page, search: self.search },
      });
    },
    // 控制全选，全不选
    handleSelectAll(status) {
      this.$refs.selection.selectAll(status);
    },

    /**
     * 删除当前角色
     * 同页面刷新不建议使用路由，再此请求数据
     * const定义的变量不可修改
     */
    remove(param) {
      this.modal1 = true;
      this.roleid = param;
    },
    ok() {
      const self = this; // 将this对象附到一个不可更改的变量中~
      axios({
        method: 'post',
        url: '/admin/admin/adminRoleDel',
        data: { id: self.roleid.row.id },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限

          // eslint-disable-next-line eqeqeq
          if (response.data.code == 1) {
            const numm = response.data.count;
            const mo = numm % 10; // 判断剩余总数摸10是否是0
            const pagepage = Math.ceil(numm / 10);
            self.data6.splice(self.roleid.index, 1); // 如果把那个1改成0，就是不删除
            if (
              self.roleid.index == 0 &&
              self.putpage != 1 &&
              mo == 0 &&
              self.putpage != pagepage
            ) {
              self.putpage -= 1;
            }
            self.$store.commit('adminRoleInfoAgain', self.putpage, self.search);
            self.$Message.success(response.data.msg);
          } else {
            self.$Message.error(response.data.msg);
          }
        })
        .catch((response) => {
          // self.$Message.error(response.data.msg);
        });
    },
    cancel() {
      this.modal1 = false;
    },
  },
};
